package com.lpwa.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.lpwa.common.utils.StringUtils;
import com.lpwa.system.domain.*;
import com.lpwa.system.mapper.AreaDeptMapper;
import com.lpwa.system.mapper.AreaPersonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lpwa.system.mapper.AreaMapper;
import com.lpwa.system.service.IAreaService;
import com.lpwa.common.core.text.Convert;

/**
 * 区域 服务层实现
 * 
 * @author tanmu
 * @date 2019-08-07
 */
@Service
public class AreaServiceImpl implements IAreaService 
{
	@Autowired
	private AreaMapper areaMapper;
	@Autowired
	private AreaDeptMapper areaDeptMapper;
	@Autowired
	private AreaPersonMapper areaPersonMapper;
	/**
     * 查询区域信息
     * 
     * @param areaId 区域ID
     * @return 区域信息
     */
    @Override
	public Area selectAreaById(Long areaId)
	{
	    return areaMapper.selectAreaById(areaId);
	}
	
	/**
     * 查询区域列表
     * 
     * @param area 区域信息
     * @return 区域集合
     */
	@Override
	public List<Area> selectAreaList(Area area)
	{
	    return areaMapper.selectAreaList(area);
	}
	
    /**
     * 新增区域
     * 
     * @param area 区域信息
     * @return 结果
     */
	@Override
	public int insertArea(Area area)
	{
	    return areaMapper.insertArea(area);
	}
	
	/**
     * 修改区域
     * 
     * @param area 区域信息
     * @return 结果
     */
	@Override
	public int updateArea(Area area)
	{
		// 删除区域与部门关联
		areaDeptMapper.deleteAreaDeptById(area.getAreaId());
		// 删除区域与人员关联
		areaPersonMapper.deleteAreaPersonById(area.getAreaId());
		//新增区域部门信息
		insertAreaDept(area);
		//新增区域人员信息
		insertAreaPerson(area);
	    return areaMapper.updateArea(area);
	}
	/**
	 * 新增区域部门信息
	 *
	 * @param area
	 */
	public int insertAreaDept(Area area)
	{
		int rows = 1;
		// 新增区域与部门管理
		List<AreaDept> list = new ArrayList<AreaDept>();
		String deptIds = area.getAssociationDept();
		if (!StringUtils.isEmpty(deptIds)){
			List<String> deptIdList = StringUtils.StringToArrayList(deptIds,",");
			for (String deptId : deptIdList) {
				AreaDept areaDept = new AreaDept();
				areaDept.setAreaId(area.getAreaId());
				areaDept.setDeptId(Long.parseLong(deptId));
				list.add(areaDept);
			}
			if (list.size() > 0)
			{
				rows = areaDeptMapper.batchAreaDept(list);
			}
		}

		return rows;
	}
	/**
	 * 新增区域人员信息
	 *
	 * @param area
	 */
	public int insertAreaPerson(Area area)
	{
		int rows = 1;
		// 新增区域与人员管理
		List<AreaPerson> list = new ArrayList<AreaPerson>();
		String personIds = area.getAssociationPerson();
		if (!StringUtils.isEmpty(personIds)){
			List<String> personIdList = StringUtils.StringToArrayList(personIds,",");
			for (String personId : personIdList) {
				AreaPerson areaPerson = new AreaPerson();
				areaPerson.setAreaId(area.getAreaId());
				areaPerson.setPersonId(personId);
				list.add(areaPerson);
			}
			if (list.size() > 0)
			{
				rows = areaPersonMapper.batchAreaPerson(list);
			}
		}

		return rows;
	}

	/**
     * 删除区域对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteAreaByIds(String ids)
	{
		return areaMapper.deleteAreaByIds(Convert.toStrArray(ids));
	}

	@Override
	public int changeStatus(Area area) {
		return areaMapper.updateArea(area);
	}

}
